package cz.zcu.fav.ratdroid.adapter;

import java.util.ArrayList;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.opengl.Visibility;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.caldroid.CaldroidGridAdapter;

import cz.zcu.fav.ratdroid.R;
import cz.zcu.fav.ratdroid.bean.Occupation;
import cz.zcu.fav.ratdroid.bean.Occupations;

public class CaldroidRATAdapter extends CaldroidGridAdapter
{
	private Occupations dates;

	public CaldroidRATAdapter(Context context, int month, int year,
			ArrayList<DateTime> disableDates,
			ArrayList<DateTime> selectedDates, DateTime minDateTime,
			DateTime maxDateTime, Occupations dates)
	{
		super(context, month, year, disableDates, selectedDates, minDateTime,
				maxDateTime);
		this.dates = dates;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View cellView = convertView;

		if (convertView == null)
		{
			cellView = inflater.inflate(R.layout.custom_cell, null);
		}
		int topPadding = cellView.getPaddingTop();
		int leftPadding = cellView.getPaddingLeft();
		int bottomPadding = cellView.getPaddingBottom();
		int rightPadding = cellView.getPaddingRight();

		TextView tv1 = (TextView) cellView.findViewById(R.id.tv1);

		// Get dateTime of this cell
		DateTime dateTime = this.datetimeList.get(position);
		Resources resources = context.getResources();

		// Set color of the dates in previous / next month
		if (dateTime.getMonthOfYear() != month)
		{
			tv1.setTextColor(resources
					.getColor(com.caldroid.R.color.caldroid_darker_gray));
		} else
		{
			if (dates != null)
			{
				// data from server are loaded so we can color dates
				Occupation occ = dates.getOccupation(dateTime.getDayOfMonth());
				if (occ != null)
				{
					tv1.setTextColor(Color.RED);
					TextView tv2 = (TextView) cellView.findViewById(R.id.tv2);
					tv2.setVisibility(View.VISIBLE);
					tv2.setText(occ.getCount() + "x");

				} else
				{
					tv1.setTextColor(resources
							.getColor(com.caldroid.R.color.caldroid_white));
				}
			} else
			// not ready yet
			{
				tv1.setTextColor(resources
						.getColor(com.caldroid.R.color.caldroid_white));
			}
		}

		// Customize for today
		if (dateTime.toLocalDate().equals(LocalDate.now()))
		{
			cellView.setBackgroundResource(com.caldroid.R.drawable.red_border_gray_bg);
			if (dates != null
					&& (dates.getOccupation(dateTime.getDayOfMonth()) != null))
			{
				tv1.setTextColor(Color.RED);
			} else
			{
				tv1.setTextColor(Color.BLACK);
			}
		}

		tv1.setText("" + dateTime.getDayOfMonth());

		// Somehow after setBackgroundResource, the padding collapse.
		// This is to recover the padding
		cellView.setPadding(leftPadding, topPadding, rightPadding,
				bottomPadding);

		return cellView;
	}

}
